Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
If statement with multiple || conditions

I am looping through an array of objects and trying to see if there is a better way to write this (i feel like there is).

This is my function below.

  getCallInfo = (callSch: any, security: any) => {
    const callObj = {
      CallTimingType: 'Not Callable',
      isCheckMark: null,
    };

    if (!security.IsCallable) return callObj;

    callSch.Calls.forEach((el: { CallTimingType: string }) => {
      if (el.CallTimingType === CallTimingType.SpecificDates) {
        callObj.CallTimingType = 'Discrete';
        callObj.isCheckMark = true;
      }
      if (
        el.CallTimingType === CallTimingType.AnyTime ||
        el.CallTimingType === CallTimingType.Monthly ||
        el.CallTimingType === CallTimingType.OnPaymentDates ||
        el.CallTimingType === CallTimingType.AnyInterestAdjustmentDate
      ) {
        callObj.CallTimingType = 'Continuous';
        callObj.isCheckMark = true;
      }
    });

    return callObj;
  };

basically what is doing is returning the default object if security.IsCallable is false but if true looping through the array of objects and setting the default object to different values. What I am basically doing is trying to refactor this. CallTimingType is an object with these enum values. Any thoughts?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You could use Array.prototype.some() to iterate and test if el.CallTimingType matches any value.

if (
  el.CallTimingType === CallTimingType.AnyTime ||
  el.CallTimingType === CallTimingType.Monthly ||
  el.CallTimingType === CallTimingType.OnPaymentDates ||
  el.CallTimingType === CallTimingType.AnyInterestAdjustmentDate
)

Can be rewritten as:

const callTimingTypes = [
     CallTimingType.AnyTime,
     CallTimingType.Monthly,
     CallTimingType.OnPaymentDates,
     CallTimingType.AnyInterestAdjustmentDate
];

if (callTimingTypes.some((t) => el.CallTimingType === t))
about 4 years ago · Santiago Trujillo Report

0

You could rewrite your clause with switch case operator to make it clear:

callSch.Calls.forEach((el: { CallTimingType: string }) => {
  switch (el.CallTimingType) {
    case CallTimingType.SpecificDates:
      callObj.CallTimingType = 'Discrete';
      callObj.isCheckMark = true;
      break;
    case CallTimingType.AnyTime:
    case CallTimingType.Monthly:
    case CallTimingType.OnPaymentDates:
    case CallTimingType.AnyInterestAdjustmentD:
      callObj.CallTimingType = 'Continuous';
      callObj.isCheckMark = true;
      break;
  }
});

Alternatively, you could use Set:

callSch.Calls.forEach((el: { CallTimingType: string }) => {
  const discreteSet = new Set([CallTimingType.SpecificDates]);
  const continuousSet = new Set([
    CallTimingType.AnyTime,
    CallTimingType.Monthly,
    CallTimingType.OnPaymentDates,
    CallTimingType.AnyInterestAdjustmentD,
  ]);

  if (discreteSet.has(el.CallTimingType)) {
    callObj.CallTimingType = 'Discrete';
    callObj.isCheckMark = true;
  } else if (continuousSet.has(el.CallTimingType)) {
    callObj.CallTimingType = 'Continuous';
    callObj.isCheckMark = true;
  }
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!